<?php

namespace App\Http\Controllers\Web;

use App\Models\News;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class NewsController extends Controller
{
    public function index(Request $request)
    {
        $size = $request->input('size', 10);
        $query = News::query()->orderByDesc("publish_date");
        $search_text = $request->input("search_text");
        $type = $request->input("type");
        $author = $request->input("author");
        $publish_date_start = $request->input("publish_date_start");
        $publish_date_end = $request->input("publish_date_start");
        $isRecommend = $request->input("isRecommend");
        $query->when($search_text, function ($query) use ($search_text) {
            $query->where('title', "like", "%{$search_text}%");
        });
        if ($type) {
            $query->where('type', $type);
        }
        if ($isRecommend) {
            $query->where('isRecommend', $isRecommend);
        }
        $query->when($author, function ($query) use ($author) {
            $query->where('author', "like", "%{$author}%");
        });
        $query->when($publish_date_start, function ($query) use ($publish_date_start) {
            $query->where('publish_date', ">=", $publish_date_start);
        });
        $query->when($publish_date_end, function ($query) use ($publish_date_end) {
            $query->where('publish_date', "<=", $publish_date_end);
        });

        return $this->successPaginate($query->paginate($size));
    }

    public function requestData(Request $request)
    {
        $requestData = $this->validate($request, [
            'title' => 'required',
            'title_en' => 'nullable',
            'cover_url' => 'required',
            'cover_url_en' => 'nullable',
            'tags' => 'nullable',
            'tags_en' => 'nullable',
            'abstract' => "nullable",
            'abstract_en' => 'nullable',
            "content" => 'required',
            "content_en" => 'nullable',
            "author" => 'required',
            "author_en" => 'nullable',
            'source' => 'required',
            'source_en' => 'nullable',
            'publish_date' => "required",
            "type" => 'required',
            "viewVolume" => 'nullable',
            "isRecommend" => 'nullable'
        ]);
        return $requestData;
    }

    public function show($id)
    {
        $new = News::query()->findOrFail($id);
        return $this->success($new);
    }

    public function store(Request $request)
    {
        $new = new News;
        $requestData = $this->requestData($request);
        $new->fill($requestData);
        $new->save();
        return $this->success($requestData);
    }

    public function update(Request $request, $id)
    {
        $new = News::query()->findOrFail($id);
        $requestData = $this->requestData($request);
        $new->fill($requestData);
        $new->save();
        return $this->success($requestData);
    }

    public function viewVolume($id)
    {
        $new = News::query()->findOrFail($id);
        $viewVolume = $new->viewVolume;

        $new->viewVolume = $viewVolume + 1;
        $new->save();
        return $this->success($new);
    }

    public function destroy($id)
    {
        $new = News::query()->findOrFail($id);
        $new->delete();
    }
}
